package com.shijun.tankwar;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/**
 * Created by zhushijun on 2018/1/12 0012.
 */

public class ContentView extends SurfaceView implements SurfaceHolder.Callback, Runnable {
    public static final String TAG = "ContentView";
    private Canvas mainCanvas;
    private SurfaceHolder surfaceHolder;
    private Paint tankPaint;

    private TankEntity tankInfo;

    private boolean isMove;
//    private boolean isTurn;

    private boolean isGameStart;
//    private int startX;
//    private int startY;
//    private int currentX;
//    private int currentY;
//    private double maxMoveOrientation = π * (5d / 180d);//最大转角15度
    private float tankOrientation = 0;

    public static final double π = Math.PI;

    public ContentView(Context context) {
        super(context);
        initView();
    }

    public ContentView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public ContentView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
        mainCanvas = new Canvas();
        surfaceHolder = getHolder();
        surfaceHolder.addCallback(this);
        tankPaint = new Paint();
        Bitmap tankBodyBitmap = BitmapFactory.decodeResource(getContext().getResources(), R.mipmap.tank_body);
        Bitmap tankBoreBitmap = BitmapFactory.decodeResource(getContext().getResources(), R.mipmap.tank_bore);
        tankInfo = new TankEntity(1001,5, 200, 200, 0, 0, tankBodyBitmap,tankBoreBitmap,0,0,0);
        mainCanvas.drawBitmap(tankInfo.getImg(), 300, 300, tankPaint);
        mainCanvas.drawBitmap(tankInfo.getBoreBitmap(), 300, 300, tankPaint);

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        isGameStart = true;
        new Thread(this).start();
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

    }

    @Override
    public void run() {
        while (isGameStart) {
            draw();
        }
    }

    private void setTankMove(double orientation,double boreOrientation, boolean isMove) {
        Matrix matrixBody = new Matrix();
        Matrix matrixBore = new Matrix();
        int offsetX = tankInfo.getImg().getWidth() / 2;
        int offsetY = tankInfo.getImg().getHeight() / 2;
        matrixBody.postRotate(-tankOrientation - 270, offsetX, offsetY);
        matrixBore.postRotate((float) -boreOrientation-270, offsetX, offsetY);
        if (isMove) {
            int addX = (int) (Math.cos(orientation) * tankInfo.getSpeed());
            int addY = -(int) (Math.sin(orientation) * tankInfo.getSpeed());
            tankInfo.setStartX(tankInfo.getStartX() + addX);
            tankInfo.setStartY(tankInfo.getStartY() + addY);
        }
        matrixBody.postTranslate(tankInfo.getStartX(), tankInfo.getStartY());
        matrixBore.postTranslate(tankInfo.getStartX(), tankInfo.getStartY());
        mainCanvas.drawBitmap(tankInfo.getImg(), matrixBody, null);
        mainCanvas.drawBitmap(tankInfo.getBoreBitmap(), matrixBore, null);
    }


    private void draw() {
        try {
            mainCanvas = surfaceHolder.lockCanvas();
            mainCanvas.drawColor(Color.WHITE);
            setTankMove(tankInfo.getOrientation(),tankInfo.getTankShootOrientation(), isMove);
        } catch (Exception e) {
        } finally {
            if (mainCanvas != null)
                surfaceHolder.unlockCanvasAndPost(mainCanvas);
        }
    }

    public void refreshTankBody(double tankMoveOrientation, float tankOrientation, int type) {
        if (type == ControlMoveView.TYPE_STOP) {
            isMove = false;
        } else {
            isMove = true;
            tankInfo.setOrientation(tankMoveOrientation);
            this.tankOrientation = tankOrientation;
        }
    }

    public void refreshTankBore(double tankMoveOrientation) {
        tankInfo.setTankShootOrientation(tankMoveOrientation);
    }
}
